﻿#include <iostream>
#include <stdio.h>
#include <string>
#include <unordered_map>
#include "CtpTrader.h"
#include "CtpMarket.h"
#include "TickToKlineHelper.h"

using namespace std;

// 链接库


// ---- 全局变量 ---- //
// 公共参数
TThostFtdcBrokerIDType gBrokerID = "9999";                         // 模拟经纪商代码
TThostFtdcInvestorIDType gInvesterID = "191516";                         // 投资者账户名
TThostFtdcPasswordType gInvesterPassword = "tianxin@4399*";                     // 投资者密码

// 行情参数
CThostFtdcMdApi* g_pMdUserApi = nullptr;      // 行情指针
char gMdFrontAddr[] = "tcp://101.226.253.32:42213";               // 模拟行情前置地址
const char *g_pInstrumentID[] = { "MA301", "m2301" };      // 行情合约代码列表，中、上、大、郑交易所各选一种
int instrumentNum = 4;                                             // 行情合约订阅数量
unordered_map<string, TickToKlineHelper> g_KlineHash;              // 不同合约的k线存储表

// 交易参数
CThostFtdcTraderApi* g_pTradeUserApi = nullptr;                    // 交易指针
char gTradeFrontAddr[] = "tcp://218.202.237.33:10203";            // 模拟交易前置地址
TThostFtdcInstrumentIDType g_pTradeInstrumentID = "MA301";        // 所交易的合约代码
TThostFtdcDirectionType gTradeDirection = THOST_FTDC_D_Sell;       // 买卖方向
TThostFtdcPriceType gLimitPrice = 22735;                           // 交易价格

int main()
{


	// 初始化行情线程
	cout << "初始化行情..." << endl;
	g_pMdUserApi = CThostFtdcMdApi::CreateFtdcMdApi();   // 创建行情实例
	CtpMarket* pMdUserSpi = new CtpMarket;               // 创建行情回调实例
	g_pMdUserApi->RegisterSpi(pMdUserSpi);               // 注册事件类
	g_pMdUserApi->RegisterFront(gMdFrontAddr);           // 设置行情前置地址
	g_pMdUserApi->Init();                                // 连接运行



	//// 初始化交易线程
	cout << "初始化交易..." << endl;
	g_pTradeUserApi = CThostFtdcTraderApi::CreateFtdcTraderApi(); // 创建交易实例
	CtpTrader* pTradeSpi = new CtpTrader;               // 创建交易回调实例
	g_pTradeUserApi->RegisterSpi(pTradeSpi);                      // 注册事件类
	g_pTradeUserApi->SubscribePublicTopic(THOST_TERT_RESTART);    // 订阅公共流
	g_pTradeUserApi->SubscribePrivateTopic(THOST_TERT_RESTART);   // 订阅私有流
	g_pTradeUserApi->RegisterFront(gTradeFrontAddr);              // 设置交易前置地址
	g_pTradeUserApi->Init();

	// 连接运行

	CThostFtdcInputOrderField ord = { 0 };
	strcpy_s(ord.BrokerID, "9999");
	strcpy_s(ord.InvestorID, "164578");
	strcpy_s(ord.ExchangeID, "DCE");
	strcpy_s(ord.InstrumentID, "pp2201");
	strcpy_s(ord.UserID, "123132");
	ord.OrderPriceType = THOST_FTDC_OPT_LimitPrice;//限价
	ord.Direction = THOST_FTDC_D_Buy;//买
	ord.CombOffsetFlag[0] = THOST_FTDC_OF_Open;//开
	ord.CombHedgeFlag[0] = THOST_FTDC_HF_Speculation;//投机
	ord.LimitPrice = 100;
	ord.VolumeTotalOriginal = 1;
	ord.TimeCondition = THOST_FTDC_TC_GFD;///当日有效
	ord.VolumeCondition = THOST_FTDC_VC_AV;///任意数量
	ord.MinVolume = 1;
	ord.ContingentCondition = THOST_FTDC_CC_Immediately;
	ord.StopPrice = 0;
	ord.ForceCloseReason = THOST_FTDC_FCC_NotForceClose;
	ord.IsAutoSuspend = 0;
	g_pTradeUserApi->ReqOrderInsert(&ord, 20210001);

	CThostFtdcOrderField ordf = {};




	while (true)
	{

	}

	// 等到线程退出
	g_pMdUserApi->Join();
	delete pMdUserSpi;
	g_pMdUserApi->Release();

	g_pTradeUserApi->Join();
	delete pTradeSpi;
	g_pTradeUserApi->Release();

	// 转换本地k线数据
	TickToKlineHelper tickToKlineHelper;
	tickToKlineHelper.KLineFromLocalData("market_data.csv", "K_line_data.csv");

	getchar();
	return 0;
}